package com.wxl.common.room.helper

import com.wxl.common.AppConstant
import com.wxl.common.AppContext
import com.wxl.common.room.database.DataBase
import com.wxl.common.room.entity.*
import com.wxl.common.util.ObservableUtil
import io.reactivex.ObservableOnSubscribe

class DBHelper {

    companion object {

        /**
         * 存入未申请爆破的数据
         */
        fun insertUnApplyBlastData(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg entitys: UnApplyBlastEntity
        ) {
            if (entitys.isNotEmpty()) {
                ObservableUtil.doNext(ObservableOnSubscribe<Boolean> {
                    DataBase.getInstance(AppContext.appContext.getApplication())
                        .unApplyBlastDao.insert(*entitys)
                    it.onNext(true)
                }, callback)
            } else {
                callback.onNext(true)
            }
        }

        /**
         * 删除未申请爆破记录
         */
        fun deleteUnApplyBlastData(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg entitys: UnApplyBlastEntity
        ) {
            if (entitys.isNotEmpty()) {
                ObservableUtil.doNext(ObservableOnSubscribe<Boolean> {
                    DataBase.getInstance(AppContext.appContext.getApplication())
                        .unApplyBlastDao.delete(*entitys)
                    it.onNext(true)
                }, callback)
            } else {
                callback.onNext(true)
            }
        }

        /**
         * 查询已经授权的数据
         */
        fun queryAppledBlastData(callback: ObservableUtil.Companion.OnNextCallback<List<UnApplyBlastEntity>>) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                it.onNext(
                    DataBase.getInstance(AppContext.appContext.getApplication())
                        .unApplyBlastDao.query(1)
                )
            }, callback)
        }


        /**
         * 查询未授权的数据
         */
        fun queryUnAuthBlastData(callback: ObservableUtil.Companion.OnNextCallback<List<UnApplyBlastEntity>>) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                it.onNext(
                    DataBase.getInstance(AppContext.appContext.getApplication())
                        .unApplyBlastDao.query(0)
                )
            }, callback)
        }

        /**
         * 插入用户信息
         */
        fun insertUser(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg user: UserEntity
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .userDao.insertUser(*user)
                it.onNext(true)
            }, callback)
        }


        /**
         * 删除用户信息
         */
        fun deleteUser(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg user: UserEntity
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .userDao.deleteUser(*user)
                it.onNext(true)
            }, callback)
        }

        /**
         * 更新用户信息
         */
        fun updateUser(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg user: UserEntity
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .userDao.updateUser(*user)
                it.onNext(true)
            }, callback)
        }

        /**
         * 查询用户信息
         */
        fun queryUser(callback: ObservableUtil.Companion.OnNextCallback<List<UserEntity>>) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                it.onNext(
                    DataBase.getInstance(AppContext.appContext.getApplication())
                        .userDao.queryUser()
                )
            }, callback)
        }

        /**
         * 查询用户信息
         */
        fun queryUserById(
            id: String,
            callback: ObservableUtil.Companion.OnNextCallback<List<UserEntity>>
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                it.onNext(
                    DataBase.getInstance(AppContext.appContext.getApplication())
                        .userDao.queryUserById(id)
                )
            }, callback)
        }

        /**
         * 查询控制器信息
         */
        fun queryDetonator(callback: ObservableUtil.Companion.OnNextCallback<List<DetonatorEntity>>) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                val count =
                    DataBase.getInstance(AppContext.appContext.getApplication()).detonatorDao
                        .queryDetonatorCount()
                if (count == 0) {
                    it.onNext(arrayListOf())
                    return@ObservableOnSubscribe
                }
                it.onNext(
                    DataBase.getInstance(AppContext.appContext.getApplication())
                        .detonatorDao.queryDetonator()
                )
            }, callback)
        }

        /**
         * 更新控制器信息
         */
        fun updateDetonator(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg mDetonator: DetonatorEntity
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .detonatorDao.updateDetonator(*mDetonator)
                it.onNext(true)
            }, callback)
        }


        /**
         * 删除采集器信息
         */
        fun deleteDetonator(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg mDetonator: DetonatorEntity
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .detonatorDao.deleteDetonator(*mDetonator)
                it.onNext(true)
            }, callback)
        }

        /**
         * 插入采集器信息
         */
        fun insertDetonator(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg mDetonator: DetonatorEntity
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .detonatorDao.insertDetonator(*mDetonator)
                it.onNext(true)
            }, callback)
        }


        /**
         * 清空控制器信息
         */
        fun clearDetonator(callback: ObservableUtil.Companion.OnNextCallback<Boolean>) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                val queryDetonator = DataBase.getInstance(AppContext.appContext.getApplication())
                    .detonatorDao.queryDetonator().toList()
                deleteDetonator(object : ObservableUtil.Companion.OnNextCallback<Boolean>() {
                    override fun onNext(t: Boolean) {
                        it.onNext(t)
                    }
                }, *queryDetonator.toTypedArray())
            }, callback)
        }


        /**
         * 插入采集器信息
         */
        fun insertCollector(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg mCollectorBean: CollectorBean
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .collectorDao.insertCollector(*mCollectorBean)
                it.onNext(true)
            }, callback)
        }


        /**
         * 删除采集器信息
         */
        fun deleteCollector(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg mCollectorBean: CollectorBean
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .collectorDao.deleteCollector(*mCollectorBean)
                it.onNext(true)
            }, callback)
        }


        /**
         * 查询采集器信息
         */
        fun queryCollector(callback: ObservableUtil.Companion.OnNextCallback<List<CollectorBean>>) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                val queryCollector =
                    DataBase.getInstance(AppContext.appContext.getApplication())
                        .collectorDao.queryCollector()
                it.onNext(queryCollector)
            }, callback)
        }


        /**
         * 清除所有的采集器信息并插入新信息
         */
        fun clearInsertCollector(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            mCollectorBean: List<CollectorBean>
        ) {
            queryCollector(object : ObservableUtil.Companion.OnNextCallback<List<CollectorBean>>() {
                override fun onNext(t: List<CollectorBean>) {
                    if (t.isNotEmpty()) {
                        deleteCollector(object :
                            ObservableUtil.Companion.OnNextCallback<Boolean>() {
                            override fun onNext(t: Boolean) {
                                insertCollector(callback, *mCollectorBean.toTypedArray())
                            }
                        }, *t.toList().toTypedArray())
                    } else {
                        insertCollector(callback, *mCollectorBean.toTypedArray())
                    }
                }
            })
        }


        /**
         * 写入未授权文件信息
         */
        fun insertAuthTrem(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg datas: AuthorizeTermEntity
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .authorizeTermDao.insertAuthorizeTerm(*datas)
                it.onNext(true)
            }, callback)
        }


        /**
         * 查询未授权文件信息
         */
        fun queryAuthTrem(callback: ObservableUtil.Companion.OnNextCallback<List<AuthorizeTermEntity>>) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                it.onNext(
                    DataBase.getInstance(AppContext.appContext.getApplication())
                        .authorizeTermDao.queryAuthorizeTerm()
                )
            }, callback)
        }


        /**
         * 插入雷管数据
         */
        fun insertLeiGuan(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg lg: LeiGuanEntity
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .leiGuanDao.insertLeiGuan(*lg)
                it.onNext(true)
            }, callback)
        }


        /**
         * 更新雷管数据
         */
        fun updateLeiGuan(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg lg: LeiGuanEntity
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .leiGuanDao.updateLeiGuan(*lg)
                it.onNext(true)
            }, callback)
        }

        /**
         * 删除雷管数据
         */
        fun deleteLeiGuan(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg lg: LeiGuanEntity
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .leiGuanDao.deleteLeiGuan(*lg)
                it.onNext(true)
            }, callback)
        }

        /**
         * 插入新雷管数据
         */
        fun insertNewLeiGuanData(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg lg: LeiGuanEntity
        ) {
            queryLeiGuanEntity(object :
                ObservableUtil.Companion.OnNextCallback<List<LeiGuanEntity>>() {
                override fun onNext(t: List<LeiGuanEntity>) {
                    deleteLeiGuan(object : ObservableUtil.Companion.OnNextCallback<Boolean>() {
                        override fun onNext(t: Boolean) {
                            insertLeiGuan(callback, *lg)
                        }
                    }, *t.toTypedArray())
                }
            })
        }

        /**
         * 查询未授权文件信息
         */
        fun queryLeiGuanEntity(callback: ObservableUtil.Companion.OnNextCallback<List<LeiGuanEntity>>) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                it.onNext(
                    DataBase.getInstance(AppContext.appContext.getApplication())
                        .leiGuanDao.queryLeiGuan()
                )
            }, callback)
        }


        /**
         * 插入雷管数据
         */
        fun insertLeiGuanDelay(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg lg: LeiGuanDelayEntity
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .leiGuanDelayDao.insertLeiGuan(*lg)
                it.onNext(true)
            }, callback)
        }


        /**
         * 更新雷管数据
         */
        fun updateLeiGuanDelay(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg lg: LeiGuanDelayEntity
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .leiGuanDelayDao.updateLeiGuan(*lg)
                it.onNext(true)
            }, callback)
        }

        /**
         * 删除雷管数据
         */
        fun deleteLeiGuanDelay(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg lg: LeiGuanDelayEntity
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .leiGuanDelayDao.deleteLeiGuan(*lg)
                it.onNext(true)
            }, callback)
        }

        /**
         * 插入新雷管数据
         */
        fun insertNewLeiGuanDelayData(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg lg: LeiGuanDelayEntity
        ) {
            queryLeiGuanDelayEntity(object :
                ObservableUtil.Companion.OnNextCallback<List<LeiGuanDelayEntity>>() {
                override fun onNext(t: List<LeiGuanDelayEntity>) {
                    deleteLeiGuanDelay(object : ObservableUtil.Companion.OnNextCallback<Boolean>() {
                        override fun onNext(t: Boolean) {
                            insertLeiGuanDelay(callback, *lg)
                        }
                    }, *t.toTypedArray())
                }
            })
        }

        /**
         * 查询未授权文件信息
         */
        fun queryLeiGuanDelayEntity(callback: ObservableUtil.Companion.OnNextCallback<List<LeiGuanDelayEntity>>) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                it.onNext(
                    DataBase.getInstance(AppContext.appContext.getApplication())
                        .leiGuanDelayDao.queryLeiGuan()
                )
            }, callback)
        }

        /**
         * 查询未授权文件信息
         */
        fun clearLeiGuanDelayEntity() {
            ObservableUtil.doNext(ObservableOnSubscribe {
                val list = DataBase.getInstance(AppContext.appContext.getApplication())
                    .leiGuanDelayDao.queryLeiGuan()
                deleteLeiGuanDelay(object : ObservableUtil.Companion.OnNextCallback<Boolean>() {
                    override fun onNext(t: Boolean) {

                    }
                }, *list.toTypedArray())
            }, object : ObservableUtil.Companion.OnNextCallback<Boolean>() {
                override fun onNext(t: Boolean) {

                }
            })
        }

        /**
         * 查询所有合同
         */
        fun queryContract(callback: ObservableUtil.Companion.OnNextCallback<List<ContractInfoEntity>>) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                it.onNext(
                    DataBase.getInstance(AppContext.appContext.getApplication())
                        .contractInfoDao.query()
                )
            }, callback)
        }

        /**
         * 添加合同
         */
        fun insertContract(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg cis: ContractInfoEntity
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .contractInfoDao.insert(*cis)
                it.onNext(true)
            }, callback)
        }

        /**
         * 删除合同
         */
        fun deleteContract(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg cis: ContractInfoEntity
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .contractInfoDao.delete(*cis)
                it.onNext(true)
            }, callback)
        }


        /**
         * 保存新的合同信息
         */
        fun setNewContract(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg cis: ContractInfoEntity
        ) {
            queryContract(object :
                ObservableUtil.Companion.OnNextCallback<List<ContractInfoEntity>>() {
                override fun onNext(t: List<ContractInfoEntity>) {
                    deleteContract(object : ObservableUtil.Companion.OnNextCallback<Boolean>() {
                        override fun onNext(t: Boolean) {
                            insertContract(callback, *cis)
                        }
                    }, *t.toTypedArray())
                }
            })
        }

        /**
         * 保存爆破日志
         */
        fun insertBlastRecord(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg br: BlastRecordEntity
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .blastRecordDao.insert(*br)
                it.onNext(true)
            }, callback)
        }

        /**
         * 查询爆破日志
         */
        fun queryBlastRecord(callback: ObservableUtil.Companion.OnNextCallback<List<BlastRecordEntity>>) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                it.onNext(
                    DataBase.getInstance(AppContext.appContext.getApplication())
                        .blastRecordDao.query()
                )
            }, callback)
        }


        /**
         * 查询爆破日志
         */
        fun queryBlastRecordPage(page:Int,callback: ObservableUtil.Companion.OnNextCallback<List<BlastRecordEntity>>) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                it.onNext(
                    DataBase.getInstance(AppContext.appContext.getApplication())
                        .blastRecordDao.queryPage(page,AppConstant.PAGE_SIZE)
                )
            }, callback)
        }

        /**
         * 查询总条数
         */
        fun queryBlastRecordTotalCount(callback: ObservableUtil.Companion.OnNextCallback<Int>){
            ObservableUtil.doNext(ObservableOnSubscribe {
                it.onNext(
                    DataBase.getInstance(AppContext.appContext.getApplication())
                        .blastRecordDao.count()
                )
            }, callback)
        }

        /**
         * 查询未上传成功的记录
         */
        fun queryBlastRecordUnLoad(callback: ObservableUtil.Companion.OnNextCallback<List<BlastRecordEntity>>){
            ObservableUtil.doNext(ObservableOnSubscribe {
                it.onNext(
                    DataBase.getInstance(AppContext.appContext.getApplication())
                        .blastRecordDao.queryUnLoad()
                )
            }, callback)
        }

        /**
         * 删除爆破日志
         */
        fun deleteBlastRecord(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg br: BlastRecordEntity
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .blastRecordDao.delete(*br)
                it.onNext(true)
            }, callback)
        }


        /**
         * 更新爆破日志
         */
        fun updateBlastRecord(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg br: BlastRecordEntity
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .blastRecordDao.update(*br)
                it.onNext(true)
            }, callback)
        }

        /**
         * 插入操作日志
         */
        fun insertOperateLog(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg log: LogEntity
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                val count = DataBase.getInstance(AppContext.appContext.getApplication())
                    .operateLogDao.count()
                if (count >= 50) {
                   val t = DataBase.getInstance(AppContext.appContext.getApplication())
                        .operateLogDao.queryEnd()
                    DataBase.getInstance(AppContext.appContext.getApplication())
                        .operateLogDao.delete(*t.toTypedArray())
                    DataBase.getInstance(AppContext.appContext.getApplication())
                        .operateLogDao.insert(*log)
                    it.onNext(true)
                } else {
                    DataBase.getInstance(AppContext.appContext.getApplication())
                        .operateLogDao.insert(*log)
                    it.onNext(true)
                }
            }, callback)
        }

        /**
         * 查询操作日志
         */
        fun queryOperateLog(callback: ObservableUtil.Companion.OnNextCallback<List<LogEntity>>) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                it.onNext(
                    DataBase.getInstance(AppContext.appContext.getApplication())
                        .operateLogDao.query()
                )
            }, callback)
        }

        /**
         * 删除操作日志
         */
        fun deleteOperateLog(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg log: LogEntity
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .operateLogDao.delete(*log)
                it.onNext(true)
            }, callback)
        }

        /**
         * 清除操作日志
         */
        fun clearOperateLog() {
            ObservableUtil.doNext(ObservableOnSubscribe {
                queryOperateLog(object :
                    ObservableUtil.Companion.OnNextCallback<List<LogEntity>>() {
                    override fun onNext(t: List<LogEntity>) {
                        ObservableUtil.doNext(ObservableOnSubscribe { oos ->
                            DataBase.getInstance(AppContext.appContext.getApplication())
                                .operateLogDao.delete(*t.toTypedArray())
                            oos.onNext(true)
                        }, object : ObservableUtil.Companion.OnNextCallback<Boolean>() {
                            override fun onNext(t: Boolean) {
                                it.onNext(true)
                            }
                        })
                    }
                })
            }, object : ObservableUtil.Companion.OnNextCallback<Boolean>() {
                override fun onNext(t: Boolean) {

                }
            })
        }


        /**
         * 查询常见问题列表
         */
        fun queryAskList(callback: ObservableUtil.Companion.OnNextCallback<List<QuestionItemEntity>>) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                it.onNext(
                    DataBase.getInstance(AppContext.appContext.getApplication())
                        .qusetionItemDao.queryQuestionItemEntity()
                )
            }, callback)
        }


        /**
         * 插入新的常见问题列表
         */
        fun setNewAskList(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg item: QuestionItemEntity
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                val list = DataBase.getInstance(AppContext.appContext.getApplication())
                    .qusetionItemDao.queryQuestionItemEntity()
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .qusetionItemDao.deleteUserQuestionItemEntity(*list.toTypedArray())
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .qusetionItemDao.insertQuestionItemEntity(*item)
                it.onNext(true)
            }, callback)
        }

        /**
         * 查询常见问题
         */
        fun queryAsk(callback: ObservableUtil.Companion.OnNextCallback<List<QuestionEntity>>) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                it.onNext(
                    DataBase.getInstance(AppContext.appContext.getApplication())
                        .qusetionDao.queryQuestionEntity()
                )
            }, callback)
        }

        /**
         * 查询常见问题
         */
        fun queryAskById(
            callback: ObservableUtil.Companion.OnNextCallback<List<QuestionEntity>>,
            id: String
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                it.onNext(
                    DataBase.getInstance(AppContext.appContext.getApplication())
                        .qusetionDao.queryQuestionById(id)
                )
            }, callback)
        }

        /**
         * 查询常见问题
         */
        fun updateAsk(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg item: QuestionEntity
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .qusetionDao.updateQuestionEntity(*item)
                it.onNext(true)
            }, callback)
        }

        /**
         * 查询常见问题
         */
        fun insertAsk(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg item: QuestionEntity
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .qusetionDao.insertQuestionEntity(*item)
                it.onNext(true)
            }, callback)
        }

        /**
         * 查询常见问题
         */
        fun setNewAsk(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg item: QuestionEntity
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                val list = DataBase.getInstance(AppContext.appContext.getApplication())
                    .qusetionDao.queryQuestionById(item[0].id)
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .qusetionDao.deleteUserQuestionEntity(*list.toTypedArray())
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .qusetionDao.insertQuestionEntity(*item)
                it.onNext(true)
            }, callback)
        }


        /**
         * 插入已经爆破的雷管数据
         */
        fun insertBlastedLeiGuan(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg item: LeiGuanBlastEntity
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .blastDao.insertLeiGuan(*item)
                it.onNext(true)
            }, callback)
        }


        /**
         * 删除已经爆破的雷管数据
         */
        fun deleteBlastedLeiGuan(
            callback: ObservableUtil.Companion.OnNextCallback<Boolean>,
            vararg item: LeiGuanBlastEntity
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                DataBase.getInstance(AppContext.appContext.getApplication())
                    .blastDao.deleteLeiGuan(*item)
                it.onNext(true)
            }, callback)
        }

        /**
         * 查询已经爆破的雷管数据
         */
        fun queryBlastedLeiGuan(
            callback: ObservableUtil.Companion.OnNextCallback<List<LeiGuanBlastEntity>>,
            blastId: String
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe {
                it.onNext(
                    DataBase.getInstance(AppContext.appContext.getApplication())
                        .blastDao.queryLeiGuan(blastId)
                )
            }, callback)
        }

    }
}